<template>
  <div>
    <!-- 对话框更换标签 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%" v-dialog-drag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="旧RFID编码" prop="oldRfidCode">
          <span>
            <el-input v-model="formData.oldRfidCode" disabled style="width: 200px;"/>
          </span>
          <span style="margin-left: 20px;">
            <el-button size="mini" type="text" icon="el-icon-magic-stick" @click="lightFlash(formData.oldRfidCode, 1)"
                       v-hasPermi="['hs:rfid:change']">点亮
          </el-button>
          </span>
        </el-form-item>
        <el-form-item label="新RFID编码" prop="newRfidCode">
          <el-input v-model="formData.newRfidCode" placeholder="请输入新RFID编码" style="width: 200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {change} from "@/api/hs/rfid";
import {lightFlash} from "@/api/hs/scan";

export default {
  name: "RFIDChangeForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        oldRfidCode: undefined,
        newRfidCode: undefined,
      },
      // 表单校验
      formRules: {
        oldRfidCode: [{required: true, message: '旧RFID编码不能为空', trigger: 'blur'}],
        newRfidCode: [{required: true, message: '新RFID编码不能为空', trigger: 'blur'}],
      },
    };
  },
  methods: {
    lightFlash,
    /** 打开弹窗 */
    async open(rfidCode) {
      this.dialogVisible = true;
      this.reset();
      this.formData.oldRfidCode = rfidCode;
      this.title = "更换标签";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 添加的提交
        await change(data);
        this.$modal.msgSuccess("更换标签成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        oldRfidCode: undefined,
        newRfidCode: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
